﻿/* From http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript */
function getUrlParms() {
    var urlParams = {};

    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
        urlParams[d(e[1])] = d(e[2]);

    return urlParams;
}

/* From http://blog.pengoworks.com/index.cfm/2009/4/21/Fixing-jQuerys-slideDown-effect-ie-Jumpy-Animation */
function slideToggle(el, bShow, dur) {
    var $el = $(el);
    var height;
    var visible = $el.is(":visible");

    if (arguments.length == 1) {
        bShow = !visible;
    }

    if (bShow == visible) {
        return false;
    }

    height = $el.show().height();

    if (!visible) {
        $el.hide().css({ height: 0 });
    }

    if (bShow) {
        $el.show().animate({ height: height }, { duration: dur });
    } 
    else {
        $el.animate({ height: 0 }, { duration: dur, complete: function () {
                $el.hide().css("height", "");
            }
        });
    }
}

function parseCurrency(sender, maxValue) {
    var currencyValue = Number($(sender).val().replace(/[^0-9\.]+/g, ""));

    if (currencyValue < 0) {
        currencyValue = 0;
    }
    else if (currencyValue > maxValue) {
        currencyValue = maxValue;
    }

    $(sender).val(currencyValue.toMoney(2));
}

function setCheckBoxes(className, value) {
    $("." + className).find("input").each(function () {
        $(this).prop("checked", value);
    });
}

function isWhitespace(node) {
    return node.nodeType == 3 && /^\s*$/.test(node.data);
}

// String handling
function pad(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }

    return str;
}

function trim(str) {
    var newStr = str + "";
    return newStr.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

String.format = function (frmt, args) {
    // Works similar to String.Format in the .NET framework
    for (var x = 0; x < args.length; x++) {
        frmt = frmt.replace("{" + x + "}", args[x + 1]);
    }

    return frmt;
}

String.prototype.toNumber = function () {
    return Number(this.replace(/[^0-9\.]+/g, ""));
}

Number.prototype.toMoney = function (decimals, decimal_sep, thousands_sep) {
    var n = this,
    c = isNaN(decimals) ? 2 : Math.abs(decimals), // If decimal is zero we must take it, it means user does not want to show any decimals
    d = decimal_sep || '.', // If no decimal separator is passed we use a period as the default (we MUST use a decimal separator)

    // According to http://stackoverflow.com/questions/411352/how-best-to-determine-if-an-argument-is-not-sent-to-the-javascript-function
    // the fastest way to check for not defined parameter is to use typeof value === 'undefined' 
    // rather than doing value === undefined.
    t = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep, // If you don't want ot use a thousands separator you can pass empty string as thousands_sep value

    sign = (n < 0) ? '-' : '',

    // Extracting the absolute value of the integer part of the number and converting to string
    i = parseInt(n = Math.abs(n).toFixed(c)) + '',

    j = ((j = i.length) > 3) ? j % 3 : 0;
    return sign + "$" + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : '');
}

// Message handling
function displayError(error) {
    displayMessage(error.get_message(), "MessageID", "MessagePanelID");
}

//function displayMessage(msg, msgKey, msgPanelKey) {
//    $("#" + msgKey).html(msg);
//    $("#" + msgPanelKey).css("display", "block");
//}

//function clearMessagePanel(msgKey, msgPanelKey) {
//    $("#" + msgKey).html("");
//    $("#" + msgPanelKey).css("display", "none");
//}
